<?php
namespace Admin\Controller;
use Think\Controller;
//接口管理
class ApiController extends Controller {
    //
    /**
     * 添加分类
     * @param $post_id post_id
     * @return 
     */
    public function addCategory(){
        $data['cat_id']=$_POST['cat_id'];
        $data['parent_id']=$_POST['parent_id'];
        $data['cat_name']=$_POST['cat_name'];
        $re = M('Category')->add($data);
    }
    /**
     * 修改分类
     * @param $post_id post_id
     * @return 
     */
    public function editCategory(){
        $data=$_POST;
        $re = M('category')->save($data);       
    }
    /**
     * 删除分类
     * @param $cat_id
     * @return 
     */
    public function delCategory(){
         $cat_id = $_POST['cat_id'];
         $re = M('category')->where("cat_id = ".$cat_id)->delete();
         
    }
  
 
    //添加商品(平台)
    public function addGoods(){

        $data['goods_id']=$_POST['goods_id'];
        $data['goods_name']=$_POST['goods_name'];
        $data['market_price']=$_POST['market_price'];
        $data['shop_price']=$_POST['shop_price'];
        $data['goods_number']=$_POST['goods_number'];
        $data['goods_sn']=$_POST['goods_sn'];
        $data['cat_id']=$_POST['cat_id'];
        $re = M('Cat_goods')->add($data);
    }
    //同步商品图片
    public function addGoodsPhoto(){
        $data=I('post.');
        $re=M('Cat_goods')->save($data);
    }
    //修改商品(平台)
    public function updateGoods(){
        //dump($_GET);exit;
        $data['goods_id']=$_POST['goods_id'];
        $data['goods_name']=$_POST['goods_name'];
        $data['market_price']=$_POST['market_price'];
        $data['shop_price']=$_POST['shop_price'];
        $data['goods_sn']=$_POST['goods_sn'];
        $data['goods_number']=$_POST['goods_number'];        
        $data['cat_id']=$_POST['cat_id'];
        $re = M('Cat_goods')->save($data);
        
    }
    //删除商品(标记为删除并未真正删除)(商铺/平台通用)
    public function delGoods(){
        $data['goods_id']=$_POST['goods_id'];
        $data['is_delete']=1;

        $re = M('Cat_goods')->save($data);
         
    }
    //彻底删除商品((商铺/平台通用))
    public function deleteGoods(){
        $id=$_POST['goods_id'];
        
        $re = M('Cat_goods')->delete($id);
         
    }
    //从回收站恢复商品((商铺/平台通用))
    public function recoverGoods(){
        $data['goods_id']=$_POST['goods_id'];
        $data['is_delete']=0;
        $re = M('Cat_goods')->save($data);        
    }
    //商铺添加商品
    public function supplierAddGoods(){
        $data['goods_id']=$_POST['goods_id'];
        $data['goods_name']=$_POST['goods_name'];
        $data['market_price']=$_POST['market_price'];
        $data['shop_price']=$_POST['shop_price'];
        $data['goods_number']=$_POST['goods_number'];
        $data['goods_sn']=$_POST['goods_sn'];
        $data['cat_id']=$_POST['cat_id'];
        $data['supplier_id']=$_POST['supplier_id'];
        
        $re = M('Cat_goods')->add($data);
        
    }
    //商铺修改商品
    public function supplierUpdateGoods(){
        $data['goods_id']=$_POST['goods_id'];
        $data['goods_name']=$_POST['goods_name'];
        $data['market_price']=$_POST['market_price'];
        $data['shop_price']=$_POST['shop_price'];
        $data['goods_sn']=$_POST['goods_sn'];
        $data['goods_number']=$_POST['goods_number'];
        $data['cat_id']=$_POST['cat_id'];
        $data['supplier_id']=$_POST['supplier_id'];
        
        $re = M('Cat_goods')->save($data);

        
     }
//同步商铺信息 
    public function addSupplier(){
        $data=I('post.');
        $re = M('Cat_supplier')->add($data);
    }
    //删除商铺
    public function delSupplier(){
        $id=$_POST['supplier_id'];
        $re = M('Cat_supplier')->delete($id);

        if ($re) {
             //同时删除该店铺下的所有商品
            M("Cat_goods")->where("supplier_id={$id}")->delete();   
         }

    }
    //商品图片相册添加图片时同步
    public function addProductPhoto(){
        $data=$_POST;
        foreach($data as $k=>$v){
        $data['img_id']=$v['img_id'];
        $data['goods_id']=$v['goods_id'];
        $data['img_url']=$v['img_url'];
        $data['img_desc']=$v['img_desc'];
        $data['thumb_url']=$v['thumb_url'];
        $data['img_original']=$v['img_original'];
        $re=M("Goods_gallery")->add($data);
        }

    }
    //商品相册添加图片时同步
    public function addOneProductPhoto(){
        $data=I('post.');
        $re=M("Goods_gallery")->add($data);
        

    }
    //删除商品相册的图片
    public function deleteProductPhoto(){
        $id=$_POST['img_id'];
        $re = M('Goods_gallery')->delete($id);
    }
}
